// JavaScript Document
var pay = {
	//创建订单
	createOrder: function () {
		var data = bp.get_data();
		$.commonAjax({
			url: 'B067',
			type: 'GET',
			data: data,
			isShowError: true,
			success: function (res) {
				console.log(res.data);
				$('.info-list:eq(0)').text('商品名称：' + res.data.auctionName);
				$('.info-list:eq(1)').text('订单编号：' + res.data.code);
				$('.info-list:eq(2)').find('span').text('￥' + res.data.payAmount);
				if (res.data.vip == null || res.data.vip == 0) {

				}
				orderCode = res.data.code;
			}, error: function (res) {
				$.layermsg(res.msg);
			}
		});
	},//VIP支付 
	payVIP: function () {
		var data = { orderCode: orderCode }
		$.commonAjax({
			url: 'B067',
			type: 'GET',
			data: data,
			isShowError: true,
			success: function (res) {
				console.log(res.data);
			}, error: function (res) {
				$.layermsg(res.msg);
			}
		});
	}, 
	//线下支付
	pay_offline: function (filePath) {
		var data = { orderCode: orderCode, payChannelCode: 100001, filePath: filePath }
		$.commonAjax({
			url: 'B059',
			type: 'POST',
			data: data,
			isShowError: true,
			success: function (res) {
				$.layermsg(res.msg);
			}, error: function (res) {
				$.layermsg(res.msg);
			}
		});
	},//第三方支付
	 payAllinpay: function (payChannelCode) {
		var data = { orderCode: orderCode, payChannelCode: payChannelCode }
		$.commonAjax({
			url: 'B097',
			type: 'POST',
			data: data,
			isShowError: true,
			success: function (res) {
				console.log(res.data);
				if (res && res.code == 200) {
					var payment = res.data;
					var serviceUrl = payment.serviceUrl;
					var formData = payment.requestOrder;
					pay.gotoAllinpay(serviceUrl, formData);
				} else {
					$.layermsg(res.msg);
				}
			}, error: function (res) {
				$.layermsg(res.msg);
			}
		});
	},// 支付跳转
	gotoAllinpay: function (url, data) {

		console.log(url)
		console.log(data)
		var $form = $("<form/>");
		$form.attr('method', 'post');
		$form.attr('action', url);
		for (var key in data) {
			var value = data[key];
			$input = $('<input/>');
			$input.attr('name', key);
			$input.attr('value', value);
			$form.append($input);
		}
		$form.hide();
		$('body').append($form)
		$form.submit();
	},
	//支付渠道 
	pay_channel_list: function () {
		var data = {}
		$.commonAjax({
			url: 'B057',
			type: 'GET',
			data: data,
			isShowError: true,
			success: function (res) {
				payChannelCode = res.data[1].code
				$('#pay_info0').text(res.data[0].bankCard.account);
				console.log(res.data[0].bankCard.name);
				$('#pay_info1').text(res.data[0].bankCard.name);
				$('#pay_info2').text(res.data[0].bankCard.bankBranchname);
				console.log(res.data);
			}, error: function (res) {
				$.layermsg(res.msg);
			}
		});
	}
}
var orderCode;
var file = '';
var payChannelCode;
$(function () {
	//H5上传
	$('#file_pay').change(function () {
		var fd = new FormData();
		fd.append('fileType', 'payment');

		var reader = new FileReader();
		if($(this)[0].files[0] == undefined){
			return false;
		}
		reader.readAsDataURL($(this)[0].files[0]);
		fd.append(0, $(this)[0].files[0]);
		$.ajax({
			url: CONFIG.apiUrl + '/upload/documentByH5.api',
			type: 'post',
			data: fd,
			processData: false,
			contentType: false,
			beforeSend: function () {
				if (layer)
					layer.closeAll('loading');
				$.layerload();
			},
			success: function (res) {
				if (layer)
					layer.closeAll('loading');
				if (res.code && res.code != 200) {
					$.layermsg(res.msg);
					return false;
				}
				var fileList = res.data;
				if (null == fileList || fileList.length == 0) {
					$.layermsg("文件上传失败");
					return false;
				}
				file = fileList[0].path;
				$('#fuck').text('已上传')
				console.log(file);
			},
			error: function () {
				if (layer)
					layer.closeAll('loading');
				$.layermsg("图片上传异常，请重试");
			}
		});
	});
	pay.pay_channel_list();
	bp.user_info(function (data) {
		console.log(data.vip);
	});
	pay.createOrder();
	$('.subBtn').click(function () {
		if (file) {
			pay.pay_offline(file);
		} else {
			pay.payAllinpay(payChannelCode);
		}
	});
});